/*
使用方法:
获取过去24小时发布的文章数量:
[zkf_index_module_03_power_level hours="24"]
获取过去12小时发布的文章数量:
[zkf_index_module_03_power_level hours="12"]
获取过去6小时发布的文章数量:
[zkf_index_module_03_power_level hours="6"]
获取当天发布的文章数量:
[zkf_index_module_03_power_level hours="24"] (或者你可以修改逻辑来支持精确到当天)
*/
/* 获取过去指定时间内发布的文章数量(能量值显示) | 结束 */
/* 根据分类或者标签筛选文章 | 开始 */
function zkf_index_module_04_custom_category_tag_code($atts) {
// 提取短代码属性并设置默认值
$atts = shortcode_atts(array(
'category' => '',
'tag' => '',
'offset' => 0,
'limit' => 5,
'category_switch' => 'and',
'tag_switch' => 'and',
'main_switch' => 'and'
), $atts, 'zkf_index_module_04_custom_category_tag');
// 验证和过滤输入参数
$offset = max(0, intval($atts['offset']));
$limit = max(1, intval($atts['limit']));
$category_switch = in_array($atts['category_switch'], array('and', 'or')) ? $atts['category_switch'] : 'and';
$tag_switch = in_array($atts['tag_switch'], array('and', 'or')) ? $atts['tag_switch'] : 'and';
$main_switch = in_array($atts['main_switch'], array('and', 'or')) ? $atts['main_switch'] : 'and';
// 处理分类
$categories = array();
if (!empty($atts['category'])) {
$category_slugs = array_map('sanitize_text_field', explode(',', $atts['category']));
foreach ($category_slugs as $slug) {
$category = get_category_by_slug(trim($slug));
if ($category) {
$categories[] = $category->term_id;
}
}
}
// 处理标签
$tags = array();
if (!empty($atts['tag'])) {
$tag_slugs = array_map('sanitize_text_field', explode(',', $atts['tag']));
foreach ($tag_slugs as $slug) {
$tag = get_term_by('slug', trim($slug), 'post_tag');
if ($tag) {
$tags[] = $tag->term_id;
}
}
}
// 设置查询参数
$args = array(
'posts_per_page' => $limit,
'offset' => $offset,
'post_type' => 'post',
'post_status' => 'publish',
);
// 设置分类查询参数
if (!empty($categories)) {
if ($category_switch === 'and') {
$args['category__and'] = $categories;
} else {
$args['category__in'] = $categories;
}
}
// 设置标签查询参数
if (!empty($tags)) {
if ($tag_switch === 'and') {
$args['tag__and'] = $tags;
} else {
$args['tag__in'] = $tags;
}
}
// 处理主要条件
if ($main_switch === 'or') {
$args['tax_query'] = array('relation' => 'OR');
if (!empty($categories)) {
$args['tax_query'][] = array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories,
'operator' => $category_switch === 'and' ? 'AND' : 'IN',
);
}
if (!empty($tags)) {
$args['tax_query'][] = array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $tags,
'operator' => $tag_switch === 'and' ? 'AND' : 'IN',
);
}
}
// 执行查询
$query = new WP_Query($args);
// 构建输出内容
$output = '';
if ($query->have_posts()) {
$output .= '
';
while ($query->have_posts()) {
$query->the_post();
$title = get_the_title();
$title = esc_html($title);
if (mb_strlen($title, 'utf-8') > 36) {
$title = mb_substr($title, 0, 36, 'utf-8') . '...';
}
$output .= '- ' . $title . '
';
}
$output .= '
';
} else {
$output = '没有找到相关文章。';
}
// 恢复全局 $post 变量
wp_reset_postdata();
return $output;
}
add_shortcode('zkf_index_module_04_custom_category_tag', 'zkf_index_module_04_custom_category_tag_code');
/*
用法:
短代码:[zkf_index_module_04_custom_category_tag category="分类1,分类2" tag="标签1,标签2" offset="0" limit="5" category_switch="or" tag_switch="or" main_switch="or"]
参数解释:
category:指定分类的 slug,可以是单个分类或多个分类,用逗号分隔。
tag:指定标签的 slug,可以是单个标签或多个标签,用逗号分隔。
offset:从第几篇文章开始(从 0 开始计数)。
limit:指定要获取的文章数量。
category_switch:分类条件,'and' 或 'or'。
tag_switch:标签条件,'and' 或 'or'。
main_switch:主条件,'and' 或 'or'。
用法示例:
短代码 [zkf_index_module_04_custom_category_tag category="分类1,分类2" tag="标签1,标签2" offset="0" limit="5" category_switch="or" tag_switch="or" main_switch="or"] 会获取第1到第5篇符合条件的文章。
调整 offset 和 limit 参数可以获取不同区间的文章。
*/
/* 根据分类或者标签筛选文章 | 结束 */
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/zhaokaifeng.com/wp-content/plugins/CoreEngine/index-page/index-page-main.php:215) in /www/wwwroot/zhaokaifeng.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/zhaokaifeng.com/wp-content/plugins/CoreEngine/index-page/index-page-main.php:215) in /www/wwwroot/zhaokaifeng.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/zhaokaifeng.com/wp-content/plugins/CoreEngine/index-page/index-page-main.php:215) in /www/wwwroot/zhaokaifeng.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/zhaokaifeng.com/wp-content/plugins/CoreEngine/index-page/index-page-main.php:215) in /www/wwwroot/zhaokaifeng.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/zhaokaifeng.com/wp-content/plugins/CoreEngine/index-page/index-page-main.php:215) in /www/wwwroot/zhaokaifeng.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/zhaokaifeng.com/wp-content/plugins/CoreEngine/index-page/index-page-main.php:215) in /www/wwwroot/zhaokaifeng.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"code":"rest_no_route","message":"\u672a\u627e\u5230\u5339\u914d URL \u548c\u8bf7\u6c42\u65b9\u5f0f\u7684\u8def\u7531\u3002","data":{"status":404}}